//
//  UITextView+PlaceHolder.m
//  iOS-Categories (https://github.com/shaojiankui/iOS-Categories)
//
//  Created by Jakey on 14/12/15.
//  Copyright (c) 2014年 www.skyfox.org. All rights reserved.
//
  #import "UITextView+PlaceHolder.h"
static const char *phTextView = "placeHolderTextView";
@implementation UITextView (PlaceHolder)
- (UILabel *)placeHolderLabel {
    return objc_getAssociatedObject(self, phTextView);
}
- (void)setPlaceHolderLabel:(UILabel *)placeHolderLabel {
    objc_setAssociatedObject(self, phTextView, placeHolderLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)addPlaceHolder:(NSString *)placeHolder {
    if (![self placeHolderLabel]) {
        self.delegate = self;
        UILabel *label = [[UILabel alloc] init];
        label.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        label.font = self.font;
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor lightGrayColor];
        label.userInteractionEnabled = NO;
        label.text = placeHolder;
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(8.0f);
            make.right.mas_equalTo(-10.0f);
        }];
        [self setPlaceHolderLabel:label];
    }
    if(self.text.length > 0)
        self.placeHolderLabel.hidden = YES;
}
# pragma mark -
# pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
    self.placeHolderLabel.hidden = YES;
   
}
- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text && [textView.text isEqualToString:@""]) {
        self.placeHolderLabel.hidden = NO;
    }
}

@end
